package cn.matezk.cloud.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.matezk.cloud.beans.PaymentNotify;
import cn.matezk.cloud.beans.PaymentOrderRequest;
import cn.matezk.cloud.dto.PaymentOrderDto;
import cn.matezk.cloud.model.ChargeStandard;
import cn.matezk.cloud.model.PaymentOrder;
import cn.matezk.cloud.service.IPaymentOrderService;
import cn.matezk.cloud.vo.PaymentOrderDetailVo;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("payment")
@Slf4j
public class PaymentController {
    @Autowired
    private IPaymentOrderService paymentOrderService;

    @PostMapping("order")
    public JSONObject order(@RequestBody PaymentOrderRequest paymentOrder, HttpServletRequest request) {
        return paymentOrderService.order(paymentOrder, request);
    }

    @GetMapping()
    public PageInfo<PaymentOrder> query(PaymentOrderDto paymentOrderDto, HttpServletRequest request) {
        paymentOrderDto.setPayer(request.getHeader("x-wx-openid"));
        paymentOrderDto.setState("SUCCESS");
        return paymentOrderService.query(paymentOrderDto);
    }

    @GetMapping("{orderId}")
    public List<PaymentOrderDetailVo> detail(@PathVariable String orderId) {
        return paymentOrderService.detail(orderId);
    }

    @PostMapping("notify")
    public JSONObject notify(@RequestBody PaymentNotify notify) throws InterruptedException {
        log.info(JSONUtil.toJsonStr(notify));
        paymentOrderService.notify(notify);
        return JSONUtil.createObj().set("errcode", 0).set("errmsg", "success");
    }

    @GetMapping("chargeStandard")
    public List<ChargeStandard> chargeStandard() throws InterruptedException {
        return paymentOrderService.chargeStandard();
    }

    @PostMapping("transfer")
    public JSONObject transfer(HttpServletRequest request, @RequestBody JSONObject params) {
        return paymentOrderService.transfer(request);
    }
}
